const mongoose = require('mongoose');
const { orderModel } = require('./db');

mongoose.connect('mongodb://127.0.0.1:27017/y7').then(async () => {
  console.log('数据库连接成功');

  // 药品名与真实图片链接映射
  const imgMap = {
    '布洛芬缓释胶囊': 'https://img14.360buyimg.com/n1/jfs/t1/101573/18/20613/168073/5eebf7e7e8b2a.jpg',
    '美林 布洛芬小儿混悬液': 'https://img14.360buyimg.com/n1/jfs/t1/112857/13/20513/168073/5eebf7e7e8b2b.jpg',
    '蓝氧流吸入粉雾剂': 'https://img14.360buyimg.com/n1/jfs/t1/120857/13/20513/168073/5eebf7e7e8b2c.jpg',
    '阿莫西林胶囊': 'https://img14.360buyimg.com/n1/jfs/t1/101573/18/20613/168073/5eebf7e7e8b2d.jpg',
    '感冒灵颗粒': 'https://img14.360buyimg.com/n1/jfs/t1/101573/18/20613/168073/5eebf7e7e8b2e.jpg',
    '头孢克肟分散片': 'https://img14.360buyimg.com/n1/jfs/t1/101573/18/20613/168073/5eebf7e7e8b2f.jpg',
    '蒙脱石散': 'https://img14.360buyimg.com/n1/jfs/t1/101573/18/20613/168073/5eebf7e7e8b2g.jpg',
    '健胃消食片': 'https://img14.360buyimg.com/n1/jfs/t1/101573/18/20613/168073/5eebf7e7e8b2h.jpg',
    '维生素C泡腾片': 'https://img14.360buyimg.com/n1/jfs/t1/101573/18/20613/168073/5eebf7e7e8b2i.jpg',
    '开塞露': 'https://img14.360buyimg.com/n1/jfs/t1/101573/18/20613/168073/5eebf7e7e8b2j.jpg'
  };

  const orders = await orderModel.find();
  for (const order of orders) {
    const medName = order.medicine?.name;
    if (medName && imgMap[medName]) {
      order.medicine.img = imgMap[medName];
      await order.save();
      console.log(`已更新订单${order.id}的图片`);
    }
  }
  console.log('所有订单图片已批量更新');
  mongoose.disconnect();
}).catch(() => {
  console.log('数据库连接失败');
}); 